package com.songchaojie.controller;

import com.github.pagehelper.PageInfo;
import com.songchaojie.entity.Category;
import com.songchaojie.entity.Goods;
import com.songchaojie.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    @RequestMapping("list")
    public String list(Model model, Goods goods, @RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "3")Integer pageSize){
        PageInfo<Goods> pg = goodsService.list(goods, pageNum, pageSize);
        model.addAttribute("goods",goods);
        model.addAttribute("pg",pg);
        return "list";
    }

    @GetMapping("add")
    public String add(){
        return "add";
    }
    @ResponseBody
    @RequestMapping("listcate")
    public List<Category> listCate(){
        return goodsService.listCate();
    }

    @PostMapping("add")
    public String add(Goods goods){
        System.out.println("=============="+goods);
        Integer len = goodsService.add(goods);
        if(len>0){
            return "redirect:list";
        }else{
            return "add";
        }
    }

    @GetMapping("update")
    public String update(Integer id,Model model){
        System.out.println("=========="+id);
        Goods goods = goodsService.GetById(id);
        model.addAttribute("g",goods);
        return "update";
    }
    @PostMapping("update")
    public String update(Goods goods){
        Integer len = goodsService.update(goods);
        if(len>0){
            return "redirect:list";
        }else{
            return "update";
        }
    }

}
